<template>
  <div>
    <el-autocomplete
      v-model="customerName"
      v-loading="loading"
      class="inline-input"
      :fetch-suggestions="selectCustomerList"
      :disabled="disabled"
      placeholder="请输入客户名称"
      style="width: 100%"
      clearable
      @select="handleSelectCustomer"
    >
      <template slot-scope="{ item }">
        <div>{{ item.customerName }}</div>
      </template>
      <el-button
        slot="append"
        icon="el-icon-search"
        :disabled="disabled"
        @click="openCustomerDialog"
      />
    </el-autocomplete>
    <div v-if="customerVisible">
      <customer-module ref="customerModule" :visible="customerVisible" @close="closeCustomerDialog" />
    </div>
  </div>
</template>

<script>

import { customer_list } from '@/api/base-data/customer'
import customerModule from '@/modules/customerModule.vue'

export default {
  name: 'CustomerQuery',
  components: { customerModule },
  props: {
    disabled: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      customerVisible: false,
      customerName: '',
      loading: false
    }
  },
  methods: {
    init(customerName) {
      this.customerName = customerName
    },
    resetQueryForm() {
      this.customerName = ''
    },
    // 打开客户弹窗
    openCustomerDialog() {
      this.customerVisible = true
      this.$nextTick(() => {
        this.$refs.customerModule.doPage()
      })
    },
    // 关闭客户弹窗
    closeCustomerDialog(row) {
      if (row) {
        this.customerName = row.customerName
        this.$emit('choose', row)
      }
      this.customerVisible = false
    },
    // 客户选中
    handleSelectCustomer(row) {
      if (row) {
        this.customerName = row.customerName
        this.$emit('choose', row)
      }
    },
    selectCustomerList(query, callback) {
      this.loading = true
      customer_list({ customerName: query }).then(resp => {
        if (resp.success) {
          callback(resp.data)
        }
      }).finally(() => {
        this.loading = false
      })
    }
  }
}
</script>
<style scoped>

</style>
